package com.hontech.dapapp.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.hontech.dapapp.R
import com.hontech.dapapp.utils.EmptyEdge
import com.hontech.dapapp.utils.dimenById


private class SpinnerItem2(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val tv = itemView.findViewById<TextView>(R.id.id_item_custom_sp_tv)

    fun setUp(index: Int, item: String, cb: (Int, String) -> Unit) {
        tv.text = item
        tv.setOnClickListener { cb(index, item) }
    }
}

private class SpinnerAdapter2(val items: List<String>, val cb: (Int, String) -> Unit) :
    RecyclerView.Adapter<SpinnerItem2>() {

    override fun onBindViewHolder(holder: SpinnerItem2, position: Int) {
        holder.setUp(position, items[position], cb)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpinnerItem2 {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_custom_sp, parent, false)
        return SpinnerItem2(view)
    }

    override fun getItemCount(): Int {
        return items.size
    }
}

private fun showViews(
    parent: TextView,
    title: String,
    items: List<String>,
    onSelect: (Int, String) -> Unit,
    onClose: () -> Unit
) {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.custom_spinner_window, null)
    val recyclerView = view.findViewById<RecyclerView>(R.id.id_csw_rv)
    val tv = view.findViewById<TextView>(R.id.id_csw_tv_title)
    val btnClose = view.findViewById<Button>(R.id.id_sp_btn_close)
    tv.text = title
    val window = PopupWindow(
        view,
        dimenById(R.dimen.popup_width),
        dimenById(R.dimen.popup_height),
        true
    )

    recyclerView.layoutManager =
        FlexboxLayoutManager(parent.context, FlexDirection.ROW, FlexWrap.WRAP)
    recyclerView.adapter = SpinnerAdapter2(items) { index, item ->
        onSelect(index, item)
        window.dismiss()
    }
    recyclerView.edgeEffectFactory = EmptyEdge()
    btnClose.setOnClickListener { window.dismiss() }
    window.animationStyle = R.style.PopupTheme
    window.isOutsideTouchable = true
    window.setOnDismissListener { onClose() }
    window.showAtLocation(parent, Gravity.CENTER, 0, 0)
}

class CustomSpinner(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val viewDown: View
    private val tv: TextView
    private var selectIndex = 0
    private var items: List<String>? = null
    private var title: String = ""

    var selectFunc: (String, Int) -> Unit = { s: String, i: Int -> }

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_custom_spinner, this)
        viewDown = view.findViewById(R.id.id_csp_down)
        tv = view.findViewById(R.id.id_csp_tv)
        view.setOnClickListener { onDownClick() }
    }

    private fun onDownClick() {
        if (items == null) {
            return
        }

        viewDown.setBackgroundResource(R.drawable.ic_up)
        showViews(tv, title, items!!, { index, item ->
            tv.text = item
            selectIndex = index
            selectFunc(item, index)
        }, {
            viewDown.setBackgroundResource(R.drawable.ic_down)
        })
    }

    fun setSelectIndex(index: Int) {
        if (items == null) {
            return
        }
        selectIndex = index
        tv.text = items!![index]
    }

    fun setItems(items: List<String>, title: String) {
        this.items = items
        if (items.isEmpty()) {
            tv.text = ""
        } else {
            tv.text = items[0]
        }
        selectIndex = 0
        this.title = title
    }

    fun setItems(items: List<String>) {
        this.items = items
        if (items.isEmpty()) {
            tv.text = ""
        } else {
            tv.text = items[0]
        }
        selectIndex = 0
    }

    fun selectText() = tv.text.toString()
    fun selectIndex() = selectIndex
}



